package com.yifei.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yifei.common.RoleCodeEnum;
import com.yifei.common.result.Result;
import com.yifei.model.system.SysRole;
import com.yifei.model.vo.SysRoleQueryVo;
import com.yifei.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;


@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    // 查询所有记录
    @ApiOperation("查询所有记录")
    @GetMapping("findAll")
    public Result findAllRoles() {
        List<SysRole> list = sysRoleService.list();
        return Result.ok(list);
    }


    // 逻辑删除
    @ApiOperation("逻辑删除")
    @DeleteMapping("remove/{id}")
    public Result removeRole(@PathVariable Long id) {
        boolean isSuccess = sysRoleService.removeById(id);
        return isSuccess ? Result.ok() : Result.fail();
    }


    // 分页查询
    @ApiOperation("获取分页查询列表")
    @GetMapping("{page}/{limit}")
    public Result index(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
                        @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
                        @ApiParam(name = "roleQueryVo", value = "查询对象", required = false) SysRoleQueryVo sysRoleQueryVo) {
        Page<SysRole> pageParam = new Page<>(page, limit);
        IPage<SysRole> pageModel = sysRoleService.selectPage(pageParam, sysRoleQueryVo);
        return Result.ok(pageModel);
    }


    // 添加
    @ApiOperation("添加角色")
    @PostMapping("save")
    public Result saveRole(@RequestBody SysRole sysRole) {
        boolean isSuccess = sysRoleService.save(sysRole);

        return isSuccess ? Result.ok() : Result.fail();
    }

    // 修改
    @ApiOperation("根据id修改")
    @PostMapping("updateById/{id}")
    public Result updateById(@PathVariable Long id) {
        SysRole sysRole = sysRoleService.getById(id);
        sysRole.setRoleCode(RoleCodeEnum.SYSTEM.getCode());
        sysRole.setDescription(RoleCodeEnum.SYSTEM.getMessage());
        sysRole.setUpdateTime(new Date());
        boolean isSuccess = sysRoleService.updateById(sysRole);
        return isSuccess ? Result.ok() : Result.fail();
    }

    // 根据id查询
    @ApiOperation("根据id查询")
    @PostMapping("findById/{id}")
    public Result findById(@PathVariable Long id) {
        SysRole sysRole = sysRoleService.getById(id);
        return Result.ok(sysRole);
    }

    // 批量删除操作
    @ApiOperation("批量删除")
    @DeleteMapping("batchDelete")
    public Result batchRemove(@RequestBody List<Long> ids) {

        boolean isSuccess = sysRoleService.removeByIds(ids);
        return isSuccess ? Result.ok() : Result.fail();
    }


}
